package com.kerrykidz.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.system.dao.mapper.CourseEntityMapper;
import com.kerrykidz.system.dao.model.CourseEntity;
import com.kerrykidz.system.service.ICourseService;
import com.rdp.framework.exception.RDPException;

@Service
public class CourseServiceImpl implements ICourseService {
	
	@Autowired
	private CourseEntityMapper courseMapper;

	@Override
	public List<CourseEntity> findNewCourese(String date) throws RDPException {
		return courseMapper.getNewCourese(date);
	}

	@Override
	public List<CourseEntity> findSignCourese(String date) throws RDPException {
		return courseMapper.getSignCourese(date);
	}

	@Override
	public List<CourseEntity> findExceedCourese(String date)throws RDPException {
		return courseMapper.getExceedCourese(date);
		
	}

	@Override
	public List<CourseEntity> findSaleCommodity(String date) throws RDPException {
		List<CourseEntity> resultList = new ArrayList<CourseEntity>();
		resultList = courseMapper.getSaleCommodity(date);
		return resultList;
	}

	@Override
	public List<CourseEntity> findReturnCommodity(String date) throws RDPException {
		return courseMapper.getReturnCommodity(date);
	}

	@Override
	public List<CourseEntity> findUseCoupon(String date) throws RDPException {
		List<CourseEntity> resultList = new ArrayList<CourseEntity>();
		List<CourseEntity> courseList = courseMapper.getUseCoupon(date);
		
		List<String> orderIdList = new ArrayList<String>();
		List<String> prdIdList = new ArrayList<String>();
		
		
		if(courseList != null && courseList.size() > 0 ){
			for(CourseEntity entity : courseList){
				/**提取优惠卷编号*/
				String[] couponArry = entity.getOrderCouponId().split(",");
				if(couponArry != null && couponArry.length > 0 ){
					for(String str : couponArry){
						if(StringUtils.isNotBlank(str)){
							String prdId = str.split("_")[0];
							String couponId =  str.split("_")[1];
							
							CourseEntity  entityConvert = new CourseEntity();
							entity.setOrderId(entity.getOrderId());
							entity.setPkPrdId(prdId);
							entity.setOrderCouponId(couponId);
							resultList.add(entityConvert);
							
							/**提取 礼券 价格 会员名称*/
							if(!orderIdList.contains(entity.getOrderId())){
								orderIdList.add(entity.getOrderId());
							}
							if(!prdIdList.contains(couponId)){
								prdIdList.add(couponId);
							}
						}
					}
				}
			}
			
			/**获取礼券价格*/
			if(resultList.size() > 0){
				List<CourseEntity> dbCoursePriceList = courseMapper.getUseCouponPrice(orderIdList, prdIdList);
				if(dbCoursePriceList != null && dbCoursePriceList.size() > 0 ){
					for(CourseEntity entity : resultList){
						for(CourseEntity dbEntity : dbCoursePriceList){
							if(entity.getPkPrdId().equals(dbEntity.getPkPrdId())){
								entity.setCusName(dbEntity.getCusName());
								entity.setPrice(dbEntity.getPrice());
							}
						}
					}
				}
			}
			
		}
		return resultList;
	}
	
	@Override
	public List<Map<String, Object>> findUseCouponK3(String date) throws RDPException {
		List<Map<String,Object>> resultMap = new ArrayList<Map<String,Object>>();
		List<String> orderIdList = new ArrayList<String>();
		List<String> prdIdList = new ArrayList<String>();
		List<CourseEntity> courseList = courseMapper.getUseCoupon(date);
		
		if(courseList != null && courseList.size() > 0 ){
			for(CourseEntity entity : courseList){
				
				/**提取优惠卷编号*/
				String[] couponArry = entity.getOrderCouponId().split(",");
				if(couponArry != null && couponArry.length > 0 ){
					for(String str : couponArry){
						if(StringUtils.isNotBlank(str)){
							//String prdId = str.split("-")[0];
							String couponId =  str.split("-")[1];
							
							/**提取 礼券 价格 会员名称*/
							if(!orderIdList.contains(entity.getOrderId())){
								orderIdList.add(entity.getOrderId());
							}
							if(!prdIdList.contains(couponId)){
								prdIdList.add(couponId);
							}
						}
					}
				}
			}
			if(orderIdList.size() > 0 && prdIdList.size() > 0){
				resultMap = courseMapper.getUseCouponPriceK3(orderIdList, prdIdList);
			}
		}
		return resultMap;
	}

	@Override
	public List<CourseEntity> findOverdueCoupon(String date) throws RDPException {
		List<CourseEntity> resultList = new ArrayList<CourseEntity>();
		List<CourseEntity> buyCouponAllList = courseMapper.getOverdueCoupon(date);
		if(buyCouponAllList != null && buyCouponAllList.size() > 0){
			for(CourseEntity entity : buyCouponAllList){
				
				String isFlag = courseMapper.isOverdueCoupon(entity.getOrderCouponId(), date);
				if(!StringUtils.isNotBlank(isFlag)){
					Double price = courseMapper.getOverdueCouponPrice(entity.getPkPrdId(),entity.getOrderId(),entity.getOrderCouponId());
					entity.setPrice(price);
					resultList.add(entity);
				}
			}
		}
		return resultList;
	}

	@Override
	public List<Map<String, Object>> findOverdueCouponK3(String date) throws RDPException {
		List<Map<String,Object>> resultMap = new ArrayList<Map<String,Object>>();
		List<CourseEntity> buyCouponAllList = courseMapper.getOverdueCoupon(date);
		if(buyCouponAllList != null && buyCouponAllList.size() > 0){
			for(CourseEntity entity : buyCouponAllList){
				
				String isFlag = courseMapper.isOverdueCoupon(entity.getOrderCouponId(), date);
				if(!StringUtils.isNotBlank(isFlag)){
					Map<String,Object> map = courseMapper.getOverdueCouponK3(entity.getPkPrdId(),entity.getOrderId(),entity.getOrderCouponId());
					resultMap.add(map);
				}
			}
		}
		return resultMap;
	}

	@Override
	public void updateCourseOver() throws RDPException{
		List<Map<String,Object>> overCourseList = courseMapper.getOverCourse();
		if(overCourseList != null && overCourseList.size() > 0){
			for(Map<String,Object> map : overCourseList){
				float signSum = Float.valueOf(map.get("signSum").toString());
				int kczjSum = Integer.valueOf(map.get("kczj").toString());
				String id = map.get("PK_MEMCLASS_ID").toString();
				
				if((kczjSum - signSum) > 0 && StringUtils.isNotBlank(id)){
					courseMapper.updateCusMenuClassById(id);
				}
			}
		}
	}

	
	

}
